package com.aqie.demo.utils;

import com.aqie.demo.database.JdbcCRUDByPreparedStatement;
import com.aqie.demo.enter.HkEnter;
import lombok.extern.slf4j.Slf4j;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @Author aqie
 * @Date 2021/12/10 16:57
 * @desc 定时任务方法
 */
@Slf4j
public class ScheduleUtils {
    public static void taskTwo() {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            public void run() {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                if ("22:00:00".equals(sdf.format(new Date()))) {
                    log.info("开始任务 {}", sdf.format(new Date()));
                    // 执行想服务端请求任务
                    List<HkEnter> hkEnterList = JdbcCRUDByPreparedStatement.queryAll();
                    Boolean aBoolean = HttpUtils.doPost("dorm/hkenter/saveBatch", hkEnterList);
                    log.info("请求结果 {}", aBoolean);
                }
                if ("23:00:00".equals(sdf.format(new Date()))) {
                    log.info("开始任务 {}", sdf.format(new Date()));
                    // 执行想服务端请求任务
                    List<HkEnter> hkEnterList = JdbcCRUDByPreparedStatement.queryAll();
                    Boolean aBoolean = HttpUtils.doPost("dorm/hkenter/saveBatch", hkEnterList);
                    log.info("请求结果 {}", aBoolean);
                }
            }
        }, 1000, 1000 * 60 * 60);// 2000=开始延迟时间 1000=间隔时间 1h
    }
}
